package com.cysion.study.ui.fragment

import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.cysion.study.ktbox.base.BaseFragment
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.other.startActivity_ex
import com.cysion.targetfun._subscribe
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.R
import com.cysion.study.adapter.SourceAdapter
import com.cysion.study.entity.CoursesEntity
import com.cysion.study.entity.HomeCourses
import com.cysion.study.helper.UserCache
import com.cysion.study.net.ApiClient
import com.cysion.study.ui.*
import com.cysion.study.ui.personal.ActivityManageActivity
import com.cysion.study.ui.personal.ClassPushActivity
import com.cysion.study.widget.ShowToast
import com.scwang.smartrefresh.layout.constant.RefreshState
import kotlinx.android.synthetic.main.fragment_teacher.*

/**
 * 普通教师首页  旧版首页
 * Created by chu on 2019-06-30.
 */
class TeacherFragment:BaseFragment(), View.OnClickListener {
    private lateinit var sourceAdapter: SourceAdapter
    private val mSourceList: MutableList<CoursesEntity> = mutableListOf()
    override fun getLayoutId(): Int {
        return R.layout.fragment_teacher
    }

    override fun initView() {
        tv_notice.setOnClickListener(this)
        tv_attent.setOnClickListener(this)
        tv_appro.setOnClickListener(this)
        tv_source_more.setOnClickListener(this)
        tv_activity_more.setOnClickListener(this)
        tv_pro_more.setOnClickListener(this)
        tv_today_attent.setOnClickListener(this)
        tv_comment.setOnClickListener(this)
        initRefreshLayout()
        initRecyclerView()

        flow.removeAllViews()
        flow_act.removeAllViews()
    }

    private fun initRecyclerView() {
        rl_source_list.isNestedScrollingEnabled=false
        sourceAdapter= SourceAdapter(mSourceList,context)
        rl_source_list.adapter=sourceAdapter
        rl_source_list.layoutManager=LinearLayoutManager(context)
    }

    //    初始化刷新控件
    private fun initRefreshLayout() {
        smartLayout.setEnableLoadMore(false)
        smartLayout.setOnRefreshListener {
            flow.removeAllViews()
            flow_act.removeAllViews()
            getDataList()
//            smartLayout.setEnableLoadMore(true)
        }
    }

    override fun initData() {
        super.initData()
        smartLayout.autoRefresh()
    }

    private fun getDataList() {
        Alert.loading(context)
        ApiClient.api.formalGetInfo(UserCache.token).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    stopRefresh()
                    for (item in it.courses) {
                        //今日课程
                        addTodayCouse(item)
                    }
                   /* for (act in it.activitys) {
                        //活动
                        addActivity(act)
                    }*/


                    tv_money.text = it.renewNum+"人"
                    if (it.notReadNotice == "0") {
                        tv_num.visibility = View.GONE
                    } else {
                        tv_num.visibility = View.VISIBLE
                        tv_num.text = it.notReadNotice
                    }

                }
                _onError {
                    Alert.close()
                    stopRefresh()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }
    }

    private fun addActivity(item: RefreshState) {
        val v = LayoutInflater.from(context).inflate(R.layout.flow_home_activity, flow_act, false)
        var tv_activity_title = v.findViewById<TextView>(R.id.tv_activity_title)
        var tv_time = v.findViewById<TextView>(R.id.tv_time)
        var tv_address = v.findViewById<TextView>(R.id.tv_address)
//        tv_activity_title.text=item.acName
//        tv_time.text=item.startTime
//        tv_address.text=item.address
        flow_act.addView(v)
    }

    private fun addTodayCouse(item: HomeCourses) {
        val v = LayoutInflater.from(context).inflate(R.layout.flow_home_couse, flow, false)
        var tv_data = v.findViewById<TextView>(R.id.tv_data)
        var tv_course_title = v.findViewById<TextView>(R.id.tv_course_title)
        var tv_time = v.findViewById<TextView>(R.id.tv_time)
        var tv_address = v.findViewById<TextView>(R.id.tv_address)

//        tv_data.text=item.dateStr
//        tv_course_title.text=item.courseName
//        tv_time.text=item.timeStr
        tv_address.text=item.address
        flow.addView(v)
    }


    private fun stopRefresh() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_notice -> {//通知列表
                tv_num.visibility = View.GONE
                context.startActivity_ex<NoticeActivity>()
            }
            R.id.tv_attent ->{//我的考勤
                context.startActivity_ex<AttendenceActivity>()
            }
            R.id.tv_appro ->{//审批
                context.startActivity_ex<ApprovalActivity>()
            }
            R.id.tv_source_more ->{//课堂更多  课表
                context.startActivity_ex<CourseMoreActivity>()
            }
            R.id.tv_activity_more ->{
//                ShowToast("活动更多")
                context.startActivity_ex<ActivityManageActivity>()
            }
            R.id.tv_pro_more ->{
                context.startActivity_ex<ClassPushActivity>()//课程推广
            }
            R.id.tv_today_attent ->{//今日考勤
                context.startActivity_ex<AttentActivity>()
            }
            R.id.tv_comment ->{//托管点评
                context.startActivity_ex<AttentCommentActivity>()
            }
        }
    }

   /* override fun loading() {
        Alert.loading(context)
    }

    override fun stopLoad() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
        Alert.close()
    }

    override fun error(code: Int, msg: String) {
        smartLayout.finishRefresh()
        ShowToast(msg)
    }*/
    override fun closeMvp() {
    }
}